<?php
namespace App\Login\Controller;

use Foxframe\Registry;

use Foxforms\Widget\Field\Input;

class indexController extends \Foxframe\Controller
{
	public function __construct()
	{
		parent::__construct();
		$this->view->setTheme('login');
	}

	public function index($params = array())
	{
		$this->redirect(BASE_PATH.'login/index/viewHome');
	}

	public function logout($params = array())
	{
		unset($_SESSION[SESSION_LOGIN]['usuario']);
		$this->redirect(BASE_PATH.'login/index/viewLogin');
	}

	public function viewHome($params = array())
	{
		$usuario	= unserialize($_SESSION[SESSION_LOGIN]['usuario']);
		$this->view->setTheme('dashBoard');
		$this->view->assign('aplicacoes', $this->em->getRepository('\Entities\Corp\Aplicacao')->findBy(array('fl_status' => 1)));
		$pessoa	= $this->em->getRepository('\Entities\Corp\Pessoa')->find($usuario->getIdPessoa());
		$this->view->assign('pessoa', $pessoa);
	}

	public function viewLogin($params = array())
	{
		try
		{
			if (isset($_SESSION[SESSION_LOGIN]['usuario']))
				throw new \Exception('Usuário já autenticado');

			$form = new \Foxforms\Widget\Form();
			$form->addElement(Input::create('application')->setType('hidden')->setValue('login'));
			$form->addElement(Input::create('controller')->setType('hidden')->setValue('index'));
			$form->addElement(Input::create('method')->setType('hidden')->setValue('validarLogin'));
			$form->addElement(Input::create('login[login]')->setType('text')->validate()->setPlaceHolder('CPF ou e-mail'));
			$form->addElement(Input::create('login[senha]')->validate()->setPlaceHolder('Senha')->setType('password'));
			$form->addElement(Input::create('botao')->setType('submit')->setValue('Entrar')->addClass('btn'));
			$this->view->assign('form', $form);
		}
		catch (\Exception $e)
		{
			$this->setFlash('Error', 'Erro', $e->getMessage());
			$this->redirect(BASE_PATH);
		}
	}

	public function validarLogin($params = array())
	{
		try
		{
			$usuario	= $this->em->getRepository('\Entities\Corp\PessoaFisica')->getLogin(
					$params['login']['login'], md5($params['login']['senha'])
			);
			if ($usuario instanceof \Entities\Corp\PessoaFisica)
			{
				$_SESSION[SESSION_LOGIN]['usuario']		= serialize($usuario);
				$pessoa	= $this->em->find('\Entities\Corp\Pessoa', $usuario->getIdPessoa());

				//carregando aplicações
				$_SESSION[SESSION_LOGIN]['aplicacoes']		= $this->em->getRepository('\Entities\Corp\Aplicacao')->carregarTodos();
				asort($_SESSION[SESSION_LOGIN]['aplicacoes']);
				$perfis	= array();
				foreach ($pessoa->getPerfis() as $perfil)
				{
					$perfis[$perfil->getId()]['id_perfil']	= $perfil->getIdPerfil();
					$perfis[$perfil->getId()]['ds_nome']	= $perfil->getDsNome();
				}
				$_SESSION[SESSION_LOGIN]['perfis']			= $perfis;

				if (isset($_SESSION[SESSION_LOGIN]['url']))
				{
					$url	= $_SESSION[SESSION_LOGIN]['url'];
					unset($_SESSION[SESSION_LOGIN]['url']);
				}
				else
				{
					$url	= BASE_PATH;
				}

				$this->redirect($url);
			}
			else
			{
				throw new \Exception('Usuário e/ou senha inválidos');
			}
		}
		catch (\Exception $e)
		{
			$this->setFlash('Error', 'Erro', $e->getMessage());
			$this->logout();
		}
	}
}